package leetcode.easy;

public class $733_FloodFill {

    int row;

    int col;

    int[][] image;

    int originPixel;

    boolean[][] vis;

    int color;


    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        int row = image.length;
        if (row == 0) {
            return image;
        }
        int col = image[0].length;

        int originPixel = image[sr][sc];
        boolean[][] vis = new boolean[row][col];

        this.row = row;
        this.col = col;
        this.image = image;
        this.originPixel = originPixel;
        this.vis = vis;
        this.color = color;
        dfs(sr, sc);
        return image;
    }

    private void dfs(int i, int j) {
        if (!isArea(i, j)) {
            return;
        }
        if (vis[i][j]) {
            return;
        }
        vis[i][j] = true;
        if (image[i][j] == originPixel) {
            image[i][j] = color;
            dfs(i - 1, j);
            dfs(i , j - 1);
            dfs(i + 1, j);
            dfs(i, j + 1);
        }
    }

    private boolean isArea(int i, int j) {
        return i >= 0 && i < row && j >= 0 && j < col;
    }
}
